package com.mall.db.api;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.OmsCompanyAddressMapper;
import com.mall.db.mapper.OmsOrderReturnApplyMapper;
import com.mall.dto.OmsOrderReturnApplyDto;
import com.mall.pojo.OmsCompanyAddress;
import com.mall.pojo.OmsOrderReturnApply;
import com.mall.pojo.OmsUpdateStatusParam;
import com.mall.vo.OmsOrderReturnApplyVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

@DubboService
public class OmsOrderReturnApplyApiImpl implements OmsOrderReturnApplyApi{
    @Autowired
    private OmsOrderReturnApplyMapper omsOrderReturnApplyMapper;

    @Autowired
    private OmsCompanyAddressMapper omsCompanyAddressMapper;


    @Override
    public Page<OmsOrderReturnApply> pageOrderReturnApply(OmsOrderReturnApplyDto ooD) {

        Page<OmsOrderReturnApply> page = new Page<>(ooD.getPageNum(), ooD.getPageSize());

        //添加查询条件
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ooD.getCreateTime()!=null,OmsOrderReturnApply::getCreateTime,ooD.getCreateTime());
        queryWrapper.eq(ooD.getHandleMan()!=null,OmsOrderReturnApply::getHandleMan,ooD.getHandleMan());
        queryWrapper.eq(ooD.getHandleTime()!=null,OmsOrderReturnApply::getHandleTime,ooD.getHandleTime());
        queryWrapper.eq(ooD.getId()!=null,OmsOrderReturnApply::getId,ooD.getId());
        queryWrapper.eq(ooD.getReceiverKeyword()!=null,OmsOrderReturnApply::getReturnPhone,ooD.getReceiverKeyword());
        queryWrapper.eq(ooD.getReceiverKeyword()!=null,OmsOrderReturnApply::getReturnName,ooD.getReceiverKeyword());
        queryWrapper.eq(ooD.getStatus()!=null,OmsOrderReturnApply::getStatus,ooD.getStatus());

        Page<OmsOrderReturnApply> pageRe = omsOrderReturnApplyMapper.selectPage(page, queryWrapper);

        return pageRe;
    }


    /**
     * 根据id进行查询退货信息详情
     * @param id
     * @return
     */
    @Override
    public OmsOrderReturnApplyVo findOrderReturnApplyById(Integer id) {
        //调用方法
        OmsOrderReturnApply omsOrderReturnApply = omsOrderReturnApplyMapper.selectById(id);
        //设置查询条件
        LambdaQueryWrapper<OmsCompanyAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsCompanyAddress::getId,omsOrderReturnApply.getCompanyAddressId());
        OmsCompanyAddress omsCompanyAddress = omsCompanyAddressMapper.selectOne(queryWrapper);

        //封装数据
        OmsOrderReturnApplyVo ooRAv = new OmsOrderReturnApplyVo();
        BeanUtil.copyProperties(omsOrderReturnApply,ooRAv);
        ooRAv.setCompanyAddress(omsCompanyAddress);
        return ooRAv;
    }


    /**
     * 修改订单状态
     * @param statusParam
     * @return
     */
    @Override
    public Integer updateOrderReturnApplyStatus(OmsUpdateStatusParam statusParam) {
        //添加修改条件
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(statusParam.getId()!=null,OmsOrderReturnApply::getId,statusParam.getId());
       //进行修改
        OmsOrderReturnApply omsOrderReturnApply = new OmsOrderReturnApply();
        omsOrderReturnApply.setCompanyAddressId(statusParam.getCompanyAddressId());
        omsOrderReturnApply.setHandleMan(statusParam.getHandleMan());
        omsOrderReturnApply.setHandleNote(statusParam.getHandleNote());
        omsOrderReturnApply.setReceiveMan(statusParam.getReceiveMan());
        omsOrderReturnApply.setReceiveNote(statusParam.getReceiveNote());
        omsOrderReturnApply.setReturnAmount( statusParam.getReturnAmount());
        omsOrderReturnApply.setStatus(statusParam.getStatus());

        //调用方法
        Integer updateNum = omsOrderReturnApplyMapper.update(omsOrderReturnApply, queryWrapper);
        return updateNum;
    }

    /**
     * 批量删除退货申请
     * @param ids
     * @return
     */
    @Override
    public Integer deleteOrderReturnApply(List<Integer> ids) {
        //设置查询条件
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OmsOrderReturnApply::getId,ids);
        queryWrapper.eq(OmsOrderReturnApply::getStatus,2);
        Integer deleteNum = omsOrderReturnApplyMapper.delete(queryWrapper);
        return deleteNum;
    }
}
